<template>
	<view class="mywrap">
		<view class="wrap-header">
			<image class="header-img" src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/home/logo.png">
			</image>
			<text class="header-text">
				让天下没有难懂的财税
			</text>
		</view>
		<button v-if="!isButton" class="menu-login" @click="checkLogin">
			手机号快捷登录
		</button>
		<button v-if="isButton" class="menu-login" open-type="getPhoneNumber" @getphonenumber="getphonenumber">
			手机号快捷登录
		</button>
		<view class="menu-phone" @click="loginOut">
			暂不登录
		</view>
		<view class="flex-col group_14" style="margin-top: 50rpx; padding: 0 30rpx;">
			<view class="flex-col group_15 xieyiBox" style="padding: 0;margin: 0;">
				<view class="xieyis">
					<view class="xieyi_btn">
						<checkbox-group style="position: relative;top: -20rpx;" class="radios blue"
							@change="checkChange">
							<checkbox :color="'#1C87F3'" :checked="checked" style="transform: scale(0.7);"></checkbox>
						</checkbox-group>
					</view>
					<view class="xieyi2">
						我已阅读并同意 <text
							@click="go('/pagesLogin/register/xieyis?type=user_agreement&name=小竹财税用户协议')">《小竹财税用户协议》</text>
						以及
						<text
							@click="go('/pagesLogin/register/xieyis?type=privacy_agreement&name=小竹财税隐私政策')">《小竹财税隐私政策》</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import loginApi from "@/api/login"
	export default {
		data() {
			return {
				openId: null,
				unionid: null,
				sessionKey: null,
				accessToken: null,
				checked: false,
				isButton: false,
				pathType: '',
				id: ''
			}
		},
		onLoad(e) {
			this.getUserNew()
			this.pathType = e.pathType
			console.log(this.pathType)
			this.id = e.id
		},
		methods: {

			//获取微信openId
			getUserNew() {
				uni.login({
					provider: 'weixin',
					success: res => {
						console.log(res);
						loginApi.getOpenId({
							code: res.code
						}).then(cts => {
							console.log(cts, '参数是什么');
							if (cts.code === 1) {
								uni.setStorage({
									key: 'userOpenId',
									data: cts.data
								})
								this.openId = cts.data.openid
								this.sessionKey = cts.data.session_key
							}
						})
					}
				});
			},

			//微信获取用户信息和手机号
			async getphonenumber(e) {
				console.log(e)
				if (e.detail.errMsg === "getPhoneNumber:ok") { // 用户允许或去手机号
					let inviteCode = uni.getStorageSync('inviteCode')
					let obj = {
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						session_key: this.sessionKey,
						openid: this.openId,
						p_invite_code: inviteCode,
						type: 7,
						applet_type: 7
					}
					console.log(obj, '登录时传入的参数')
					let res = await loginApi.getUserInfo(obj)
					if (res.code === 1) {
						uni.setStorageSync("token", res.data.token);
						// 获取个人信息  存到本地
						uni.setStorageSync("userInfo", res.data.userinfo)
						this.$store.state.userInfo = res.data.userinfo
						console.log('loginuserinfo',  res.data.userinfo)
						let that = this
						this.$store.dispatch('createZIM').then((zim_x) => {
							const app = getApp()
							app.getZIM(res.data.userinfo, that);
						});
						if(this.pathType == "undefined" || this.pathType == '' || this.pathType == null || this.pathType == 0) {
							uni.switchTab({
								url: "/pages/home/home"
							})
						}else {
							if(this.pathType == 1) {
								uni.redirectTo({
									url: "/pagesShop/shopDetail/shopDetail?id=" + this.id
								})
							}else if(this.pathType == 2) {
								uni.redirectTo({
									url: "/pagesShop/serviceDetail/serviceDetail?id=" + this.id
								})
							}else if(this.pathType == 3) {
								uni.redirectTo({
									url: "/pagesDemand/demandDetail/demandDetail?id=" + this.id
								})
							}
						}
					} else {
						this.$common.msgShow(res.msg);
					}
				} else {
					uni.switchTab({
						url: '/pages/home/home'
					})
				}
			},

			//检查是否阅读协议
			checkChange(e) {
				console.log(e)
				this.checked = !this.checked
				this.isButton = !this.isButton
			},

			//提示阅读协议
			checkLogin() {
				let that = this
				if (this.isButton === false) {
					uni.showModal({
						cancelColor: '#999',
						cancelText: '再看看',
						confirmColor: '#0177FD',
						confirmText: '同意',
						content: '请仔细阅读《小竹财税用户协议》和《小竹财税隐私政策》，点击同意代表您已阅读并同意',
						success: function(res) {
							if (res.confirm) {
								that.checked = true
								that.isButton = true
							}
						}
					})
				}
			},

			//前往阅读协议
			go(url) {
				uni.navigateTo({
					url: url
				})
			},

			//暂不登录
			loginOut() {
				uni.switchTab({
					url: '/pages/home/home'
				})
			}

		}
	}
</script>

<style scoped>
	.mywrap {
		height: 100vh;

	}

	.wrap-header {
		height: 630rpx;
		background-image: url('https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/home/wxLoginBg.png');
		background-repeat: no-repeat;
		background-size: cover;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.header-img {
		margin-top: 300rpx;
		width: 130rpx;
		height: 130rpx;
	}

	.header-text {
		margin-top: 40rpx;
		color: #0E0F0E;
		font-size: 42rpx;
	}

	.menu-login {
		margin: 0 auto;
		width: 660rpx;
		height: 100rpx;
		background: #1C87F3;
		border-radius: 80rpx;
		font-size: 30rpx;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.menu-phone {
		margin: 24rpx auto 0;
		width: 660rpx;
		height: 100rpx;
		background: #FFFFFF;
		border-radius: 80rpx;
		border: 1px solid #d9d9d9;
		font-size: 30rpx;
		color: #222222;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.xieyis {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		font-size: 26rpx;
		color: gray;
		/* margin-top: 20rpx; */
	}

	.xieyi_btn {
		display: flex;
		/* align-items: center; */
	}

	.xieyi2 {
		width: 600rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #59595D;
		line-height: 1.5;
		padding-left: 6rpx;
		box-sizing: border-box;
	}

	.xieyi2 text {
		color: #1C87F3;
	}
</style>